package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.*;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.ChannelArea;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.CanCheckPosition;
import com.iot08.common.entity.app.Device;
import com.iot08.common.entity.app.HeartRateRecord;
import com.iot08.common.response.Response;
import com.iot08.common.utils.CollectionUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Description: 健康管理
 * @author: Weichao
 * @time: 2021/9/1 11:20
 */
@Controller
@RequestMapping("health")
public class HealthMController extends BaseController {

    @Resource
    private HealthMService healthMService;
    @Resource
    private DeviceService deviceService;
    @Resource
    private ChannelAreaService channelAreaService;
    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;

    /**
     * 健康管理
     * @param page
     * @time: 2021/08/24 09:41
     * @author: WeiChao
     * @version 1.0
     */
    @RequestMapping("healthManagement")
    public ModelAndView healthManagement(PageVO page, HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        List devList = new ArrayList();
        SysUser user = this.getSessionUser();
        if (request.getMethod().toUpperCase().equals("POST")){
            String imei = page.getSearchValue();
            //是否符合 imei格式
            Device oneDev = deviceService.findById(imei);
            String showMsg = "没有相关数据";
            boolean canAdd = true;
            if(oneDev == null){
                mv.addObject("showMsg", showMsg);
            }else{
                //是同个渠道的才能查询,否则返回null 数据和提示
                Integer userAreaLevel = 0;
                if (user.getType() == Const.CHANNELUSER_TYPE) {
                    userAreaLevel = user.getChannelArea() == null ? 1 : user.getChannelArea().getLevel();
                    if (userAreaLevel == 1) {
                        List<Long> channelCodes = user.getChannelAff().getChannelCodes();
                        if(!channelCodes.contains(oneDev.getAgent())){
                            canAdd = false;
                            showMsg = "设备号存在，但无权限访问该设备";
                            mv.addObject("showMsg", showMsg);
                        }
                    }
                }
            }
            if (canAdd){
                devList.add(oneDev);
            }

        }
        mv.addObject("healthDevDataes", devList);
        mv.addObject("user", user);
        mv.setViewName("health/health_management");
        return mv;
    }

}
